package com.eshop;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class GenerateProductList
 */
@WebServlet("/GenerateProductList")
public class GenerateProductList extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GenerateProductList() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Connection connection 	= null;
		Statement stmt = null;
				
		ResultSet rs = null;
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		//write UI
		out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\">");
		out.println("<html>");
		out.println("<head>");
		out.println("  <title>EShop</title>");
		out.println("  <style type=\"text/css\">");
		out.println("  body { font-family: Georgia, \"Times New Roman\",Times, serif; color: white; background-color: #272e34; background-image:url('images/background.jpg'); background-repeat:no-repeat;}");
		out.println("  h1.title   {margin-bottom:0px; margin-top:0px; font-family: Chiller, Geneva, Arial,SunSans-Regular, sans-serif; font-size:200px; color: #45c5f2; position: relative; left:500px;}");
		out.println("  table.navbar { color: #45c5f2;left:600px; position:relative;}");
		out.println("  table.navbar td {  border:2px; border-style:solid; border-color:#45c5f2 #45c5f2;}");
		out.println("  table.productList{ color: #45c5f2; left:600px;  top:30px; position:relative;}");
		out.println("  a:link 	{ text-decoration:  none; color: #45c5f2  }");
		out.println("  a:visited { text-decoration:  none; color: #45c5f2  }");
		out.println(" </style>");
		out.println("</head>");
		out.println("<body>");
		
		out.println(" <h1 class=\"title\">EShop</h1>");
		
		out.println("<!-- Site navigation menu -->");
		out.println("<table class=\"navbar\">");
		out.println(" <td><a href=\"index.html\">Home page</a></td>");
		out.println(" <td><a href=\"Products.html\">Products</a></td>");
		out.println(" <td><a href=\"ShoppingCart.html\">Shopping Cart</a></td>");
		out.println(" <td><a href=\"Register.html\">Register</a></td>");
		out.println(" </table>");
		  
		out.println(" <!-- Main content -->");
		try{
			// Load (and therefore register) the Oracle Driver
			Class.forName("com.mysql.jdbc.Driver");
			// Get a Connection to the database
			connection = DriverManager.getConnection( "jdbc:mysql://localhost:3306/eshop", "root", "admin");
			// Create a Statement object
			stmt = connection.createStatement();
			  
			// Execute an SQL query, get a ResultSet
			rs = stmt.executeQuery("SELECT ProductID, ProductName, PricePerUnit, SupplierID FROM product");
			
			out.println("<table class=\"productList\" border=\"0\">");
			out.println("<tr><td> Products : </td></tr>");
			while(rs.next()) {
				out.println("<form method=\"GET\" action=\"addToChart\">");
				out.println("<tr>");
				out.println("<td>" + rs.getString("ProductName") 	+ "</td>" +
							"<td>" + rs.getString("PricePerUnit")   + "$$</td>");
				out.println("<td><INPUT TYPE=\"HIDDEN\" NAME=\"productID\" VALUE=\"" + rs.getString("ProductID") + "\"></td>");
				out.println("<td><INPUT TYPE=\"text\" NAME=\"nrOfUnits\" \"></td>");
				out.println("<td><input type=\"submit\" value=\"Add to cart\" \"></td>");
				out.println("</tr>");
				out.println("</form>");
			}
			// Display the result set as a list
			out.println("<form method=\"POST\" action=\"ShoppingCart.html\">");
			out.println("<td><input type=\"submit\" value=\"CheckOut\" \"></td>");
			out.println("</form>");		
			out.println("</table>");
			
			out.println("</BODY></HTML>");
		}
		catch(ClassNotFoundException e) {
			out.println("Couldn't load database driver: " + e.getMessage());
		}
		catch(SQLException e) {
			out.println("SQLException caught: " + e.getMessage());
		}
		finally {
			// Always close the database connection.
		try {
		if (connection != null) connection.close();
		}
		catch (SQLException ignored) { }
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
